package com.aop.controller;


import com.aop.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wzm
 * @create 2023-10-11 15:30
 * project_name:stu_sys
 */
@Slf4j
@RestController
public class CookieController {
    //模拟登录成功 通知浏览器保存cookie
    @GetMapping("/addCookie")
    public Result addCookie(HttpServletResponse response){
        //1.创建Cookie对象
        Cookie cookie = new Cookie("loginUser","zss");
        //2.通知浏览器保存cookie
        response.addCookie(cookie);
        //3.响应结果
        return Result.success();
    }

    //其他请求模拟从会话技术cookie中获取数据
    @GetMapping("/getCookie")
    public Result getCookie(HttpServletRequest request){
        //1.获取浏览器中的Cookie
        Cookie[] cookies = request.getCookies();
        //2.获取具体的cookie
        for (Cookie cookie : cookies){//cookie:key-value
            if(cookie.getName().equals("loginUser")){//key：loginUser
                System.out.println("cookie.getValue() = " + cookie.getValue());//cookie:value
            }

        }
        return Result.success("cookie找到了");
    }

}
